// pages/myOrder/index/index.js
const app = getApp()
import api from '../../../utils/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        orderData: [], //代驾的数据内容
        orderDatas: [], //审车的数据内容
        tabData: [{
            title: "待结算",
            name: 5,
            value: "待结算",
        }, {
            title: "已完成",
            name: 7,
            value: "已完成",
        }, {
            title: "已取消",
            name: 8,
            value: "已取消",
        }], //tab标题
        tabIndex: 5,
        totalpage: 0,
        isShow: false,
        params: {
            page: 1,
            /* 是	string	页码 */
            size: 10,
            status: 3, //status为3待结算，为4待评价，为5已完成，8已取消
            /* 是	string	页额 */
            type: 0 //0时代表普通订单，当为1时代表审车订单
        },
        tabIdx: '0', //0代表代驾订单，1代表审车订单
        tabDatas: [{
                title: "全部",
                name: 99,
            }, {
                title: "未接单",
                name: 0,
            }, {
                title: "进行中",
                name: 1,
            }, {
                title: "待评价",
                name: 6,
            },
            {
                title: "已完成",
                name: 7,
            }, {
                title: "已取消",
                name: 8,
            }
        ], //tab审车标题
        popShow: false,
        rateData: {
            id: '', //订单id
            store: '5', //评价的评分
            comment: '', //评分内容
            type: '1'
        }
    },
    tabChange(e) {
        //切换tab标签时触发
        var that = this;
        that.setData({
            tabIndex: e.detail.name,
            isShow: false
        })
        if (e.detail.name == 5) {
            that.setData({
                "params.page": 1,
                "params.status": 3,
                orderData: []
            })
            that.getData(5)
        } else if (e.detail.name == 7) {
            that.setData({
                "params.page": 1,
                "params.status": 5,
                orderData: []
            })
            that.getData(7)
        } else if (e.detail.name == 8) {
            that.setData({
                "params.page": 1,
                "params.status": 8,
                orderData: []
            })
            that.getData(8)
        }
    },
    cut(e) {
        //点击顶部tab栏进行切换的方法
        var type = e.currentTarget.dataset.name
        this.data.isShow = false
        if (type == 'dj') {
            this.data.tabIdx = '0'
            this.data.params.type = '0'
            this.data.params.status = 3
            this.data.orderData = []
            this.getData()
        } else {
            this.data.tabIdx = '1'
            this.data.params.type = '1'
            this.data.params.status = 99
            this.data.orderDatas = []
            this.getDatas()
        }
        this.setData({
            isShow: false,
            tabIdx: this.data.tabIdx,
            orderData: this.data.orderData,
            orderDatas: this.data.orderDatas,
            "params.type": this.data.params.type
        })
    },
    tabChanges(e) {
        //当审车订单里的tab栏改变时改变的内容
        this.data.params.page = 1
        this.data.params.status = e.detail.name
        this.data.isShow = false
        this.setData({
            "params.page": 1,
            isShow: this.data.isShow,
            orderDatas: [],
            "params.status": this.data.params.status
        })
        this.getDatas()
    },
    copy(e) {
        var sn = e.currentTarget.dataset.sn
        wx.setClipboardData({ //复制文本
            data: sn,
            success: function (res) {
                wx.showToast({
                    title: '复制成功',
                    icon: "success",
                    mask: "true" //是否设置点击蒙版，防止点击穿透
                })
            }
        })
    },
    getDatas() {
        //此处为审车订单的数据操作
        let apis = api.api;
        let url = apis.host + 'order/djOrderList';
        var lastList = this.data.orderDatas
        let data = this.data.params;
        if (this.data.isShow) {
            return false
        }
        wx.showLoading({
            title: '数据加载中'
        })
        apis.request(url, data, 'POST').then((res) => {
            if (res.code == 200) {
                wx.hideLoading()
                if (res.data.length == 0) {
                    this.setData({
                        isShow: true
                    })
                    return
                }
                var order = lastList.concat(res.data)
                this.setData({
                    orderDatas: order
                })
            }
        }).catch(function (res) {})
    },
    getData(status) {
        //此处是普通代驾订单的操作
        // status指的是当前订单的状态
        // https://dageda.api.hjdz.ltd/order/djOrderList
        let apis = api.api;
        let url = apis.host + 'order/djOrderList';
        var that = this;
        var lastList = that.data.orderData
        let data = this.data.params;
        if (that.data.isShow) {
            return
        }
        wx.showLoading({
            title: '数据加载中'
        })
        apis.request(url, data, 'POST').then(function (res) {
            if (res.code == 200) {
                wx.hideLoading()
                let data = res.data
                if (res.data.length == 0) {
                    that.setData({
                        isShow: true
                    })
                    return
                }
                let arrList = data.filter(function (item, index) {
                    if (item.status == status) {
                        let time = item.addtime.substr(-9, 6)
                        item.time = time;
                        return item
                        // data[index]['time'] = time
                        // that.data.orderData.push(data[index])
                    }
                })
                var order = that.data.orderData.concat(arrList)
                that.setData({
                    orderData: order
                })
            }
        }).catch(function (res) {})
    },
    cancelOrder(e) {
        //取消订单
        var that = this
        var id = e.currentTarget.dataset.id
        let apis = api.api;
        let url = apis.host + 'Userdjorder/exit_orderaudit';
        let data = {
            id: id
        };
        apis.request(url, data, 'POST').then((res) => {
            if (res.code == 200) {
                wx.showToast({
                    title: '取消成功',
                    icon: 'success',
                    success() {
                        that.setData({
                            orderDatas: []
                        })
                        that.getDatas()
                    }
                })
            }
        }).catch(function (res) {})
    },
    contact(e) {
        //联系代驾
        var mobile = e.currentTarget.dataset.mobile
        wx.makePhoneCall({
            phoneNumber: mobile
        })
    },
    toEvalu(e) {
        //去评价
        var id = e.currentTarget.dataset.id
        this.setData({
            "rateData.id": id,
            popShow: true
        })
    },
    onClose() {
        this.setData({
            popShow: false
        })
    },
    rateInp(e) {
        //订单评论分数
        this.setData({
            "rateData.store": e.detail,
        });
    },
    evalInp(e) {
        //订单评论内容
        this.setData({
            "rateData.comment": e.detail
        })
    },
    evalOk() {
        //点击评论弹窗中的确定
        var that = this
        let apis = api.api;
        let url = apis.host + 'Userdjorder/commentorder';
        var data = this.data.rateData
        apis.request(url, data, 'POST').then((res) => {
            if (res.code == 200) {
                wx.showToast({
                    title: '评论成功',
                    icon: 'success',
                    success() {
                        that.setData({
                            orderDatas: [],
                            popShow: false
                        })
                        that.getDatas()
                    }
                })
            }
        }).catch(function (res) {})
    },
    evalQx() {
        //点击评论弹窗中的取消
        this.setData({
            popShow: false
        })
    },
    onReachBottom() {
        var that = this;
        this.data.params.page++
        if (this.data.tabIdx == '0') {
            that.getData(this.data.tabIndex)
        } else {
            this.getDatas()
        }
        that.setData({
            "params.page": this.data.params.page,
            orderData: that.data.orderData
        })
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            orderData: [],
            orderDatas: [],
            "params.page": 1,
            isShow: false
        })
        if (this.data.tabIdx == '0') {
            this.getData(this.data.tabIndex)
        } else {
            this.getDatas()
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options) {
            if (options.type == 'checkCar') {
                this.data.tabIdx = '1'
                this.data.params.type = '1'
                this.setData({
                    tabIdx: this.data.tabIdx,
                    'params.type': this.data.params.type
                })
                this.getDatas()
            } else {
                this.getData(this.data.tabIndex)
            }
        } else {
            if (this.data.tabIdx == '0') {
                this.getData(this.data.tabIndex)
            } else {
                this.getDatas()
            }
        }
    },
    onShow() {

    }
})